package com.orange.vod.controller;

import com.orange.commonutils.result.Result;
import com.orange.servicebase.exception.MyException;
import com.orange.vod.service.VodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author orange
 * @email orangestart@yeah.net
 * @create 2021-12-20 21:45
 */
@RestController
@CrossOrigin
@Api(tags = "阿里云视频点播")
@RequestMapping("/eduVod/video")
public class VodController {

    @Autowired
    private VodService vodService;

    //上传视频到阿里云
    @ApiOperation("上传视频到阿里云")
    @PostMapping("/upload")
    public Result upload(@ApiParam("上传视频文件") MultipartFile file) {
        //返回上传视频的id
        String videoId = vodService.uploadVideoAliyun(file);
        return Result.ok().data("videoId", videoId);
    }

    //删除视频
    @ApiOperation("根据端视频id删除视频")
    @DeleteMapping("/{videoId}")
    public Result removeVideo(@ApiParam(name = "videoId", value = "云端视频id", required
            = true) @PathVariable String videoId) {
        vodService.removeVideo(videoId);
        return Result.ok().message("视频删除成功");
    }

    //批量删除视频
    @ApiOperation("批量删除视频")
    @DeleteMapping("/delete-batch")
    public Result removeVideoList(
            @ApiParam(name = "videoIdList", value = "云端视频id", required = true)
            @RequestParam("videoIdList") List<String> videoIdList){
        vodService.removeVideoList(videoIdList);
        return Result.ok().message("视频删除成功");
    }

    //根据视频id获取视频凭证
    @GetMapping("/getPlayAuth/{id}")
    public Result getPlayAuth(@PathVariable String id){
        try {
            String playAuth = vodService.getPlayAuth(id);
            return Result.ok().data("playAuth",playAuth);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(20001,"获取视频凭证失败");
        }

    }

}
